Problem Statement

Problem 1: Develop an application for preparation of a quiz and its online conduction. The key features to be considered are as follows: Develop a page for user authentication. Create some users for your application, and assign roles:
(1) Teacher
(2) Student
(3) Administrator
Implement authorization to show interfaces per user role.
Teacher can add:
(1) Question statement
(2) Answer options
(3) Correct choice
Students attempt randomized quizzes and see/store results at end.

Code Implementation

Login.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="Login" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Login</title>
</head>
<body>
    <form id="form2" runat="server">
        <div style="text-align: center; margin-top: 50px;">
            <h2>Quiz Login</h2>
            <asp:TextBox ID="txtUsername" runat="server" Placeholder="Username" /><br /><br />
            <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Placeholder="Password" /><br /><br />
            <asp:Button ID="Button1" runat="server" Text="Login" OnClick="btnLogin_Click" />
            <asp:Label ID="lblMessage" runat="server" ForeColor="Red" />
        </div>
    </form>
</body>
</html>
                

Login.aspx.vb

Partial Class Login
    Inherits System.Web.UI.Page

    Public Class AppUser
        Public Username As String
        Public Password As String
        Public Role As String

        Public Sub New()
        End Sub

        Public Sub New(u As String, p As String, r As String)
            Username = u
            Password = p
            Role = r
        End Sub
    End Class

    Protected Sub btnLogin_Click(sender As Object, e As EventArgs)
        Dim users As New List(Of AppUser) From {
            New AppUser("Irfan Hameed", "12345", "Teacher"),
            New AppUser("Hadiqa Nadeem", "12345", "Student"),
            New AppUser("Irtaza Saleem", "admin", "Admin")
        }

        Dim currentUser As AppUser = users.FirstOrDefault(Function(u) u.Username = txtUsername.Text AndAlso u.Password = txtPassword.Text)
        If currentUser IsNot Nothing Then
            Session("Username") = currentUser.Username
            Session("Role") = currentUser.Role

            Select Case currentUser.Role
                Case "Teacher"
                    Response.Redirect("Teacher.aspx")
                Case "Student"
                    Response.Redirect("Student.aspx")
                Case "Admin"
                    Response.Redirect("Admin.aspx")
            End Select
        Else
            lblMessage.Text = "Invalid username or password."
        End If
    End Sub
End Class
                

Sample Output

Login Output Login Output

Teacher Code Implementation

Teacher.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeFile="Teacher.aspx.vb" Inherits="Teacher" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Teacher - Add Questions</title>
</head>
<body>
    <form id="form3" runat="server">
        <div style="width: 600px; margin: auto;">
            <h2>Add New Question</h2>
            <asp:TextBox ID="txtQuestion" runat="server" Width="100%" TextMode="MultiLine" Rows="3" Placeholder="Enter Question Statement"></asp:TextBox><br /><br />

            <asp:TextBox ID="txtOption1" runat="server" Placeholder="Option 1" /><br />
            <asp:TextBox ID="txtOption2" runat="server" Placeholder="Option 2" /><br />
            <asp:TextBox ID="txtOption3" runat="server" Placeholder="Option 3" /><br />
            <asp:TextBox ID="txtOption4" runat="server" Placeholder="Option 4" /><br /><br />

            Correct Option (1-4): <asp:TextBox ID="txtCorrect" runat="server" /><br /><br />

            <asp:Button ID="btnAdd" runat="server" Text="Add Question" OnClick="btnAdd_Click" /><br /><br />
            <asp:Label ID="lblStatus" runat="server" ForeColor="Green" />
        </div>
    </form>
</body>
</html>
                

Teacher.aspx.vb

Partial Class Teacher
    Inherits System.Web.UI.Page

    Protected Sub btnAdd_Click(sender As Object, e As EventArgs)
        Dim correctOption As Integer
        If Not Integer.TryParse(txtCorrect.Text, correctOption) OrElse correctOption < 1 OrElse correctOption > 4 Then
            lblStatus.ForeColor = Drawing.Color.Red
            lblStatus.Text = "Please enter a valid number (1-4) for the correct option."
            Exit Sub
        End If

        Dim q As New Question With {
            .Statement = txtQuestion.Text,
            .Options = New String() {
                txtOption1.Text, txtOption2.Text, txtOption3.Text, txtOption4.Text
            },
            .CorrectIndex = correctOption - 1
        }

        Dim questions As List(Of Question)
        If Application("QuizQuestions") Is Nothing Then
            questions = New List(Of Question)
        Else
            questions = CType(Application("QuizQuestions"), List(Of Question))
        End If

        questions.Add(q)
        Application("QuizQuestions") = questions

        lblStatus.ForeColor = Drawing.Color.Green
        lblStatus.Text = "Question added successfully!"

        txtQuestion.Text = ""
        txtOption1.Text = ""
        txtOption2.Text = ""
        txtOption3.Text = ""
        txtOption4.Text = ""
        txtCorrect.Text = ""
    End Sub
End Class
                

Questions.vb

Public Class Question
    Public Property Statement As String
    Public Property Options As String()
    Public Property CorrectIndex As Integer
End Class
            

Sample Output

Teacher Output Teacher Output

Code Implementation - Student

Student.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeFile="Student.aspx.vb" Inherits="Student" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Quiz - Student</title>
</head>
<body>
    <form id="form4" runat="server">
        <div style="width: 600px; margin: auto; margin-top: 30px;">
            <h2>Quiz</h2>
            <asp:Label ID="lblQuestion" runat="server" Font-Bold="True" /><br /><br />
            <asp:RadioButtonList ID="rblOptions" runat="server" /><br />
            <asp:Button ID="btnNext" runat="server" Text="Next Question" OnClick="btnNext_Click" /><br /><br />
            <asp:Label ID="lblEnd" runat="server" Font-Bold="True" ForeColor="Green" />
        </div>
    </form>
</body>
</html>
                

Student.aspx.vb

Partial Class Student
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim questions = CType(Application("QuizQuestions"), List(Of Question))
            If questions Is Nothing OrElse questions.Count = 0 Then
                lblQuestion.Text = "No questions found. Please ask your teacher to add some."
                btnNext.Visible = False
                Return
            End If

            Session("Questions") = ShuffleList(questions)
            Session("CurrentIndex") = 0
            Session("Score") = 0
            LoadQuestion()
        End If
    End Sub

    Private Function ShuffleList(Of T)(inputList As List(Of T)) As List(Of T)
        Dim rnd As New Random()
        Return inputList.OrderBy(Function(x) rnd.Next()).ToList()
    End Function

    Private Sub LoadQuestion()
        Dim questions = CType(Session("Questions"), List(Of Question))
        Dim currentIndex = CType(Session("CurrentIndex"), Integer)

        Dim q = questions(currentIndex)
        lblQuestion.Text = q.Statement
        rblOptions.Items.Clear()

        For Each opt In q.Options
            rblOptions.Items.Add(opt)
        Next
    End Sub

    Protected Sub btnNext_Click(sender As Object, e As EventArgs)
        Dim questions = CType(Session("Questions"), List(Of Question))
        Dim currentIndex = CType(Session("CurrentIndex"), Integer)
        Dim score = CType(Session("Score"), Integer)

        If rblOptions.SelectedIndex = questions(currentIndex).CorrectIndex Then
            score += 1
            Session("Score") = score
        End If

        currentIndex += 1
        Session("CurrentIndex") = currentIndex

        If currentIndex >= questions.Count Then
            lblQuestion.Visible = False
            rblOptions.Visible = False
            btnNext.Visible = False
            lblEnd.Text = "Quiz complete! Your score: " & score & " / " & questions.Count
        Else
            LoadQuestion()
        End If
    End Sub
End Class
                

Sample Output

Student Output Student Output Student Output

Demo

Task 3 Demo - Survey
Web hosting by Somee.com